const { accountModel } = require("../models/account")
const { userModel } = require("../models/user")
const md5 = require("md5")
const { getuid } = require("../utils/auth")

class UserController {
  static async register(ctx) {
    // 参数校验
    ctx.verifyParams({
      name: { type: 'string', min: 1, max: 24, required: true },
      pass: { type: 'string', min: 6, max: 16, required: true },
    })

    // 获取参数
    const name = ctx.request.body.name
    const pass = md5(ctx.request.body.pass)

    // 业务逻辑/数据库操作
    const hasAccount = await accountModel.findOne({ name })
    if (hasAccount) {
      ctx.err({ message: "用户名已存在" })
      return
    }

    const accounts = await accountModel.insertMany({
      name,
      pass
    })

    const account = accounts[0]
    // console.log("account:", account)

    const users = await userModel.insertMany({
      _id: account._id,
      nickname: account.name
    })

    const user = users[0]
    // console.log("user:", user)

    // 返回请求结果...
    ctx.succ({
      data: {
        user
      }
    })
  }

  static async getProfile(ctx) {
    const uid = getuid(ctx)
    // const user = await userModel.findById(uid)
    const user = await userModel.findByIdAndUpdate(uid, {
      lastLoginDate: Date.now() //更新最后登录时间
    }) // 但不返回更新时间后的用户信息
    if (!user) {
      ctx.err({
        message: "用户id不存在, 用户身份异常, 请重新登录"
      })
    }

    ctx.succ({
      data: {
        user
      }
    })
  }

  static async getProfileById(ctx) {
    const uid = ctx.request.params.id
    const user = await userModel.findById(uid, {
      // 不返回私有数据
      favorites: 0,
      history: 0,
      // createDate: 0,
      lastLoginDate: 0,
    })
    if (!user) {
      ctx.result({
        code: 404,
        message: "用户不存在"
      })
    }

    ctx.succ({
      data: {
        user
      }
    })
  }

  static async changeNickname(ctx) {
    // 参数校验
    ctx.verifyParams({
      nickname: { type: 'string', min: 1, max: 24, required: true },
    })

    // 获取参数
    const nickname = ctx.request.body.nickname
    const uid = getuid(ctx)

    // 数据更新
    const user = await userModel.findByIdAndUpdate(uid, {
      nickname
    }, {
      new: true  //获取更新后的数据
    })

    // 返回结果
    ctx.succ({
      data: {
        newNickname: nickname,
        user
      }
    })
  }

  static async changeAvatarUrl(ctx) {
    // 参数校验
    ctx.verifyParams({
      avatarUrl: { type: 'string', required: true }
    })

    const avatarUrl = ctx.request.body.avatarUrl
    const uid = getuid(ctx)
    const user = await userModel.findByIdAndUpdate(uid, {
      avatarUrl
    }, {
      new: true
    })

    // 返回结果
    ctx.succ({
      data: {
        newAvatarUrl: avatarUrl,
        user
      }
    })
  }

  static async changeSignature(ctx) {
    // 参数校验
    ctx.verifyParams({
      signature: { type: 'string', max: 60, required: true }
    })

    const signature = ctx.request.body.signature
    const uid = getuid(ctx)
    const user = await userModel.findByIdAndUpdate(uid, {
      signature
    }, {
      new: true
    })

    // 返回结果
    ctx.succ({
      data: {
        newSignature: signature,
        user
      }
    })
  }

  static async changeEmail(ctx) {
    // 参数校验
    ctx.verifyParams({
      email: { type: 'email', required: true }
    })

    const email = ctx.request.body.email
    const uid = getuid(ctx)
    const user = await userModel.findByIdAndUpdate(uid, {
      email
    }, {
      new: true
    })

    // 返回结果
    ctx.succ({
      data: {
        newEmail: email,
        user
      }
    })
  }

  static async changeSex(ctx) {
    // 参数校验
    ctx.verifyParams({
      sex: { type: 'number', min: 0, max: 2, required: true }
    })

    const sex = ctx.request.body.sex
    const uid = getuid(ctx)
    const user = await userModel.findByIdAndUpdate(uid, {
      sex,
    }, {
      new: true
    })

    ctx.succ({
      data: {
        newSex: sex,
        user
      }
    })
  }
}

module.exports = {
  UserController
}